#include "Vector.h"


#ifndef NULL
#define NULL 0
#endif 

template<typename Data>
Vector<Data>::Vector()
{
	array = NULL;
	size1 = 0;
	max_size = 0;
	noResize = 0;
}


template<typename Data>
Vector<Data>::Vector(const Vector<Data>& v)
{
	allocated = v.allocated;
	if(allocated == 1)
	{
		array = (Data*) Embeddable::embed_malloc(sizeof(Data)*v.max_size);
		unsigned int i = 0;
		for(i = 0; i < v.max_size; i++)
			array[i] = v.array[i];
		//array has been allocated now hurray..
	}
	size1 = v.size1;
	max_size = v.max_size;
	noResize = v.noResize;
}

template<typename Data>
Vector<Data>& Vector<Data>::operator=(const Vector<Data>& v)
{
	if(v.array == array)
		return *this;
	if(allocated==1)
	{
		//need to deallocated the current vector.
		Embeddable::embed_free(array);
	}
	allocated = v.allocated;
	if(allocated == 1)
	{
		array = (Data*) Embeddable::embed_malloc(sizeof(Data)*v.max_size);
		unsigned int i = 0;
		for(i = 0; i < v.max_size; i++)
			array[i] = v.array[i];
		//array has been allocated now hurray..
	}
	size1 = v.size1;
	max_size = v.max_size;
	noResize = v.noResize;
	return *this;
}

template<typename Data>
Vector<Data>::Vector(unsigned char r, unsigned int max)
{
	array = (Data*) Embeddable::embed_malloc(sizeof(Data)*max);
	size1 = 0;
	max_size = max;
	noResize = r;
	allocated = 1;
}

template<typename Data>
Vector<Data>::Vector(Data* array, unsigned int size)
{
	array = array;
	size1 = 0;
	allocated = 0;
	max_size = size;
	noResize = 1;
}

template<typename Data>
Vector<Data>::~Vector()
{
	if(array != NULL && allocated == 1)
	{
		//free the array
		Embeddable::embed_free(array);
		array = NULL;
		size1 = 0;
		max_size = 0;
	}
}

template<typename Data>
void Vector<Data>::push_back(Data d)
{
	if(noResize==0 && (array == NULL || this->full() == 1))
	{
		resize();
	}
	//add the data to the array.
	if(this->full() == 0)
	{
		array[size1] = d;
		size1 = size1 + 1;
	}
	return;
	
}

template<typename Data>
Data& Vector<Data>::get(unsigned int i)
{
	return array[i];
}

template<typename Data>
Data Vector<Data>::pop_back()
{

	Data value = array[size1-1];
	size1 = size1 -1;
	return value;	
}
//Contains contents of node data type.

template<typename Data>
unsigned char Vector<Data>::empty()
{
	if(array==NULL || size1==0)
		return 1;
	else 
		return 0;
}


template<typename Data>
void Vector<Data>::resize()
{
	if(array == NULL) 
	{
		array =  (Data*) Embeddable::embed_malloc(100*sizeof(Data));
		size1 = 0;
		max_size = 100;
	}
	else
	{
		if(size1 >= max_size)
		{
			Data* array2 = array;
			array = (Data*) Embeddable::embed_malloc(max_size*2*sizeof(Data));
			int i = 0;
			for(i = 0; i < size1; i++)
				array[i] = array2[i];
			max_size = max_size*2;
			free(array2);
		}
	}
}

template<typename Data>
unsigned char Vector<Data>::full()
{
	if(array==NULL || max_size <= size1)
		return 1;
	else
		return 0;
}

template<typename Data>
unsigned int Vector<Data>::size()
{
	return size1;
}

template<typename Data>
void Vector<Data>::set(unsigned int i, Data d)
{
	array[i] = d;
}

template<typename Data>
Data& Vector<Data>::operator[](unsigned int i)
{
	return get(i);
}

